package com.honglang.module.article.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import com.honglang.dao.GenericDaoImpl;
import com.honglang.module.article.entity.BlogArticleComment;

public class BlogArticleCommentDaoImpl extends GenericDaoImpl<BlogArticleComment,Integer> implements BlogArticleCommentDao{

	/**
	 * get the  blogArticleComments of one page which comment is the rootComment
	 * @param pageSize
	 * @param pageNo
	 * @return
	 */
	public List<?> getBlogArticleRootCommentsPerPage(int pageSize,int pageNo,Integer blogArticleId){
		Session session= getHibernateTemplate().getSessionFactory().openSession();
		Criteria criteria=session.createCriteria(BlogArticleComment.class);
		int startIndex=pageSize*(pageNo-1);
		criteria.add(Restrictions.eq("blogArticle.id", blogArticleId));
		criteria.add(Restrictions.isNull("rootComment"));
		criteria.add(Restrictions.isNull("toComment"));
		criteria.addOrder(Order.asc("commentDate"));
		criteria.setFirstResult(startIndex);
		criteria.setMaxResults(pageSize);
		List<?> result=criteria.list();
		session.close();
		return result;
	}
	
	
	/**
	 * get the blogArticleRootComment rowCount ;
	 * @param blogArticleId
	 * @return
	 */
	public Integer getBlogArticleRootCommentRowCount(Integer blogArticleId){
		DetachedCriteria dc = DetachedCriteria.forClass(BlogArticleComment.class);
		dc.add(Restrictions.eq("blogArticle.id", blogArticleId));
		dc.add(Restrictions.isNull("rootComment"));
		dc.add(Restrictions.isNull("toComment"));
		dc.setProjection(Projections.rowCount());
		return (Integer)this.getHibernateTemplate().findByCriteria(dc).get(0);
	}
}
